package camelinaction.service;

import camelinaction.*;
import camelinaction.dao.Player;
import camelinaction.dao.PlayerJpaRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Optional;
import java.util.Set;

/**
 * 聊天服务
 * @author -琴兽-
 *
 */
@Component
public class ChatServiceImpl implements ChatService{

	@Autowired
	private PlayerJpaRepository playerJpaRepository;

	@Override
	public void publicChat(long playerId, String content) {
		
		Optional<Player> optionalPlayer = playerJpaRepository.findById(playerId);
		//Player player = playerJpaRepository.findByPlayerName(String.valueOf(playerId)).get(0);
		//Player player=optionalPlayer.get();
		Player player=optionalPlayer.get();
		
		//获取所有在线玩家
		Set<Long> onlinePlayers = SessionManager.getOnlinePlayers();
		
		//创建消息对象
		ChatResponse chatResponse = new ChatResponse();
		chatResponse.setChatType(ChatType.PUBLIC_CHAT);
		chatResponse.setSendPlayerId(player.getPlayerId());
		chatResponse.setSendPlayerName(player.getPlayerName());

		chatResponse.setMessage(content);
		System.out.println("ChatResponse:"+chatResponse.toString());


		//发送消息
		for(long targetPlayerId : onlinePlayers){
			//SessionManager.sendMessage(targetPlayerId, ModuleId.CHAT, ChatCmd.PUSHCHAT, chatResponse.getMessage());

			//Response response=new Response();
			//问题已经找到，必须要发送的类型是Response才行。否则无法进入到Encode中
			//SessionManager.sendMessage(targetPlayerId, chatResponse);
			//在正常返回的地方，在new response(request)，在这个地方，是程序手动设置的方式来写入参数
			SessionManager.sendMessage(targetPlayerId, ModuleId.CHAT, ChatCmd.PUSHCHAT, chatResponse);
			/*JSONObject jsonObject = (JSONObject) JSONObject.parseObject(result, Result.class).getContent();
			JSONObject.parseObject(chatResponse);
			ObjectMapper mapper=new ObjectMapper();
			mapper.
			SessionManager.sendMessage(new Response(2,101,));*/
		}

	}

	@Override
	public void privateChat(long playerId, long targetPlayerId, String content) {
		//不能和自己私聊
		if(playerId == targetPlayerId){
			throw new ErrorCodeException(ResultCode.CAN_CHAT_YOUSELF);
		}
		
		Optional<Player> player = playerJpaRepository.findById((playerId));
		
		//判断目标是否存在
		Optional<Player> targetPlayer = playerJpaRepository.findById(targetPlayerId);
		if(targetPlayer == null){
			throw new ErrorCodeException(ResultCode.PLAYER_NO_EXIST);
		}
		
		//判断对方是否在线
		if(!SessionManager.isOnlinePlayer(targetPlayerId)){
			throw new ErrorCodeException(ResultCode.PLAYER_NO_ONLINE);
		}
		
		//创建消息对象
		ChatResponse chatResponse = new ChatResponse();
		chatResponse.setChatType(ChatType.PRIVATE_CHAT);
		chatResponse.setSendPlayerId(player.get().getPlayerId());
		chatResponse.setSendPlayerName(player.get().getPlayerName());
		chatResponse.setTartgetPlayerName(targetPlayer.get().getPlayerName());
		chatResponse.setMessage(content);
		
		//给目标对象发送消息
		SessionManager.sendMessage(targetPlayerId, ModuleId.CHAT, ChatCmd.PUSHCHAT, chatResponse.getMessage());
		//给自己也回一个(偷懒做法)
		SessionManager.sendMessage(playerId, ModuleId.CHAT, ChatCmd.PUSHCHAT, chatResponse.getMessage());
		//return chatResponse;
	}
}
